ERPmean = [    10.0623
  -14.1431
   -3.6331
    3.3389
   -5.3788
    5.0906
    6.8901
    1.9458
    1.9085
    6.4343
    2.3234
    0.1193
    0.6272
   -1.0685
    0.4527
    1.7327
    4.5593
   -0.3841
   -2.5345
   -2.3424
   -5.2860
   -7.6003
   -6.7387
  -13.4234
    4.3727
    3.6803
    3.3429
   -8.9511
    3.3311
    1.6264
   -5.1466
    3.2325
    0.0666
    2.3425
   -3.4950
   -0.0828
   -2.5469
   -8.0875
   -1.1202
   -7.5274
    3.3592
   -2.5024
   -0.6195
    0.8961
   -1.3139
   -5.1371
    1.1414
    3.1315
    1.1292
   -2.5826
    3.5549
    4.6619
   -1.6563
   -2.7453
   -1.4194
   -1.8962
   -3.6241
    4.1945
   -1.1289
  -14.6778
   -6.7191
   -0.5854
   -9.0254
  -14.8564
   -4.7849
   24.0950
    9.2002
   -2.6507
  -13.9644
    3.6636
    0.3879
    3.7428
   -0.8947
   -8.2952
   -1.3302
    3.4903
    2.5763
  -14.9928
   -0.7542
   -1.1122
   -4.3460
   -2.3217 ];

n1 = 36;
n2 = 46;
% compute significance between trials
[ t df pvalboot] = statcond( { ERPmean(1:n1)' ERPmean(n1+1:end)' }, 'mode', 'bootstrap', 'naccu', 1000);

% run GLM and bootstrap
naccu = 3000;
R     = zeros(1, naccu+1); % last value is non shuffled
GL  = [ [ones(1,n1) zeros(1,n2)]' [zeros(1,n1) ones(1,n2)]'  ones(n1+n2,1) ];
pGL = pinv(GL);
for ind = 1:naccu+1
    if ind == naccu+1
         tmp = ERPmean';
    else tmp = shuffle(ERPmean)';
    end;
    params    = pGL*tmp';
    m         = corrcoef(GL*params, tmp);
    R(ind)    = m(2);
end;

% compute p-value for GLM
[surrog idx] = sort(R);
[tmp mx]     = max( idx ); % find the position of the max index = non shuffled
len = length(surrog);
pvals = 1-(mx-0.5)/len;
pvals = min(pvals, 1-pvals); % two tailed
pvals = 2*pvals; 

% print results and show figure
fprintf('GLM p-value: %2.4f    Bootstrap p-value: %2.4f)\n', pvals, min(pvalboot));
figure;plot(ERPmean); hold on;
m1 = mean(ERPmean(1:n1));
m2 = mean(ERPmean(n1+1:end)); 
plot(1:n1, m1*ones(1,n1), 'k');
plot(n1+1:n1+n2, m2*ones(1,n2), 'k');
title('First segment is condition 1 and second is condition 2');

